#include <stdio.h>
#include "mpi.h"

#define BUFFER_SIZE 32768
#define BUFFER_LIMIT (BUFFER_SIZE / sizeof(int))

int main( int argc, char *argv[] )
{
    int rank;
    void *buffer;
    int pos = 0;
    int i;
    int j;
    int good = 1;
    MPI_Status status;

    MPI_Init( &argc, &argv );
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    buffer = malloc( BUFFER_SIZE );

    if ( rank == 0 )
    {
        for ( i=0; i<BUFFER_LIMIT; i++ )
            MPI_Pack( &i, 1, MPI_INT, buffer, BUFFER_SIZE, &pos, MPI_COMM_WORLD );
        MPI_Bcast( buffer, pos, MPI_PACKED, 0, MPI_COMM_WORLD ); 
    }
    else
    {
        MPI_Bcast( buffer, BUFFER_SIZE, MPI_PACKED, 0, MPI_COMM_WORLD );
        for ( i=0; i<BUFFER_LIMIT; i++ )
        {
            MPI_Unpack( buffer, BUFFER_SIZE, &pos, &j, 1, MPI_INT, MPI_COMM_WORLD );
            if ( j != i )
                good = 0;
        }
    
        if ( good )
            printf( "Okay\n" );
        else
            printf( "Error\n" );
    }

    free( buffer );
    MPI_Finalize( );

    return 0;
}

